var t = getApp();

Page({
  data: {
    // objectMultiArray: [[{name:'按重'},{name:'按件'}]],
    // multiIndex: [ 0 ],
    // objectMultiArray: ['按重','按件'],
    store_id: '', //店铺id
    objectMultiArray: [{
      name: '按重'
    }, {
      name: '按件'
    }],
    regional: "请选择计费方式",
    showCoverLayer: !1,
    calculate_type: '', //计费方式 0按重 1按件
    dispatch_name: '', //模板名称
    first_weight: '', //首重克数
    another_weight: '', //续重克数
    first_weight_price: '', //首重价格
    another_weight_price: '', //续重价格
    first_piece: '', //首件个数
    another_piece: '', //续件个数
    first_piece_price: '', //首件价格
    another_piece_price: '', //续件价格
    template_id: '', //模板id
  },
  onLoad: function (o) {
    console.log(o, 'options')
    this.setData({
      store_id: o.store_id
    })
    var s = t._isTextEmpty(o.template_id);
    console.log(s, 'sssss')
    this.setData({
      isType: s
    })
    this._setTitle()
    if (!s) {
      this.setData({
        template_id: o.template_id
      })
      this.getDetail()
    }
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  _setTitle: function () {
    this.data.isType ? wx.setNavigationBarTitle({
      title: "模板发布"
    }) : wx.setNavigationBarTitle({
      title: "模板编辑"
    });
  },
  getDetail: function () {
    console.log(this.data.template_id, 'template_id')
    var e = this,
      i = t.getNetAddresss('HuanNiu.get_ps_muban');
    t._postNetWork({
      url: i,
      showToastIn: !1,
      data: {
        id: e.data.template_id,
      },
      success: function (t) {
        var o = t.data;
        console.log(o, 'ooooooooo')
        if (o.result == 1) {
          if (o.data) {
            e.setData({
              calculate_type: o.data.calculate_type, //计费方式 0按重 1按件
              dispatch_name: o.data.dispatch_name, //模板名称
              first_weight: o.data.first_weight, //首重克数
              another_weight: o.data.another_weight, //续重克数
              first_weight_price: o.data.first_weight_price, //首重价格
              another_weight_price: o.data.another_weight_price, //续重价格
              first_piece: o.data.first_piece, //首件个数
              another_piece: o.data.another_piece, //续件个数
              first_piece_price: o.data.first_piece_price, //首件价格
              another_piece_price: o.data.another_piece_price, //续件价格
              regional: o.data.calculate_type == 0 ? '按重' : '按件'
            })
          } else {
            wx.showToast({
              icon: "none",
              title: o.msg,
              duration: 1500
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: o.msg,
            duration: 1500
          });
        }
      },
      fail: function (t) {
        e.setData({
          is_tap: !1
        });
      }
    });
  },
  commodityconfirm: function (e) {
    console.log(e.detail.value, 'commodityconfirm')
    let index = e.detail.value
    this.setData({
      calculate_type: index,
      regional: this.data.objectMultiArray[index].name
    })
    console.log(this.data.calculate_type, this.data.regional)
  },
  tab_discount: function (o) {
    var s = this,
      e = t.getNetAddresss("plugin.store-cashier.frontend.store.goods.get-store-goods-setting");
    t._postNetWork({
      url: e,
      success: function (t) {
        var o = t.data;
        1 == o.result ? (console.log(o.data.goods_widgets.tab_discount, o.data.goods_widgets.tab_privilege, "权限", "折扣是否开启"),
          1 == o.data.goods_widgets.tab_discount && (s.setData({
            tab_discount: !0
          }), s.getGoodsJson()), 1 == o.data.goods_widgets.tab_privilege && (s.setData({
            tab_privilege: !0
          }), s.getGoodsJson())) : wx.showToast({
          icon: "none",
          title: o.msg,
          duration: 1500
        });
      },
      fail: function (t) {
        console.log(t.msg);
      }
    });
  },
  dispatchnameinp: function (t) {
    console.log(t, 'dispatchnameinp')
    var a = t.detail.value;
    this.setData({
      dispatch_name: a
    });
  },
  firstweightinp: function (t) {
    var a = t.detail.value;
    this.setData({
      first_weight: a
    });
  },
  anotherweightinp: function (t) {
    var a = t.detail.value;
    this.setData({
      another_weight: a
    });
  },
  firstweightpriceinp: function (t) {
    var a = t.detail.value;
    this.setData({
      first_weight_price: a
    });
  },
  anotherweightpriceinp: function (t) {
    var a = t.detail.value;
    this.setData({
      another_weight_price: a
    });
  },
  firstpieceinp: function (t) {
    var a = t.detail.value;
    this.setData({
      first_piece: a
    });
  },
  anotherpieceinp: function (t) {
    var a = t.detail.value;
    this.setData({
      another_piece: a
    });
  },
  firstpiecepriceinp: function (t) {
    var a = t.detail.value;
    this.setData({
      first_piece_price: a
    });
  },
  anotherpiecepriceinp: function (t) {
    var a = t.detail.value;
    this.setData({
      another_piece_price: a
    });
  },
  editTemplate: function (){
    this.setData({
      showCoverLayer: true
    })
    var e = this,
      i = t.getNetAddresss('HuanNiu.edit_ps_muban');
    t._postNetWork({
      url: i,
      showToastIn: !1,
      data: {
        id: e.data.template_id,
        dispatch_name: e.data.dispatch_name,
        calculate_type: e.data.calculate_type,
        first_weight: e.data.first_weight,
        another_weight: e.data.another_weight,
        first_weight_price: e.data.first_weight_price,
        another_weight_price: e.data.another_weight_price,
        first_piece: e.data.first_piece,
        another_piece: e.data.another_piece,
        first_piece_price: e.data.first_piece_price,
        another_piece_price: e.data.another_piece_price
      },
      success: function (t) {
        var o = t.data;
        console.log(o, 'ooooooooo')
        if (o.result == 1) {
          if (o.data) {
            wx.showToast({
              icon: "none",
              title: e.data.isType ? "上传模板成功" : "编辑成功",
              duration: 1500
            })
            wx.navigateBack({
              delta: 1
            })
          } else {
            e.setData({
              showCoverLayer: false
            })
            wx.showToast({
              icon: "none",
              title: o.msg,
              duration: 1500
            });
          }
        } else {
          e.setData({
            showCoverLayer: false
          })
          wx.showToast({
            icon: "none",
            title: o.msg,
            duration: 1500
          });
        }
      },
      fail: function (t) {
        e.setData({
          is_tap: !1
        });
      }
    });
  },
  addTemplate: function (){
    this.setData({
      showCoverLayer: true
    })
    var e = this,
      i = t.getNetAddresss('HuanNiu.add_ps_muban');
    t._postNetWork({
      url: i,
      showToastIn: !1,
      data: {
        store_id: e.data.store_id,
        dispatch_name: e.data.dispatch_name,
        calculate_type: e.data.calculate_type,
        first_weight: e.data.first_weight,
        another_weight: e.data.another_weight,
        first_weight_price: e.data.first_weight_price,
        another_weight_price: e.data.another_weight_price,
        first_piece: e.data.first_piece,
        another_piece: e.data.another_piece,
        first_piece_price: e.data.first_piece_price,
        another_piece_price: e.data.another_piece_price
      },
      success: function (t) {
        var o = t.data;
        console.log(o, 'ooooooooo')
        if (o.result == 1) {
          if (o.data) {
            // wx.navigateTo({
            //   url: '/packageC/o2o/FreightTemplate/FreightTemplate',
            // })
            wx.showToast({
              icon: "none",
              title: e.data.isType ? "上传模板成功" : "编辑成功",
              duration: 1500
            })
            wx.navigateBack({
              delta: 1
            })
          } else {
            e.setData({
              showCoverLayer: false
            })
            wx.showToast({
              icon: "none",
              title: o.msg,
              duration: 1500
            });
          }
        } else {
          e.setData({
            showCoverLayer: false
          })
          wx.showToast({
            icon: "none",
            title: o.msg,
            duration: 1500
          });
        }
      },
      fail: function (t) {
        e.setData({
          is_tap: !1
        });
      }
    });
  },

  publishEvent: function () {
    // var s = this.data
    console.log(this.data.calculate_type,'计费方式')
    if (t._isTextEmpty(this.data.dispatch_name)) {
      wx.showToast({
        title: "请填写模板名称",
        icon: "none"
      });
      return
    }
    if (this.data.regional == '请选择计费方式') {
      wx.showToast({
        title: "请选择计费方式",
        icon: "none"
      });
      return
    }
    if (this.data.calculate_type == 0) {
      if (t._isTextEmpty(this.data.first_weight)) {
        wx.showToast({
          title: "请填写首重克数",
          icon: "none"
        });
        return
      }
      if (t._isTextEmpty(this.data.another_weight)) {
        wx.showToast({
          title: "请填写续重克数",
          icon: "none"
        });
        return
      }
      if (t._isTextEmpty(this.data.first_weight_price)) {
        wx.showToast({
          title: "请填写首重价格",
          icon: "none"
        });
        return
      }
      if (t._isTextEmpty(this.data.another_weight_price)) {
        wx.showToast({
          title: "请填写续重价格",
          icon: "none"
        });
        return
      }
    } else if (this.data.calculate_type == 1) {
      if (t._isTextEmpty(this.data.first_piece)) {
        wx.showToast({
          title: "请填写首件个数",
          icon: "none"
        });
        return
      }
      if (t._isTextEmpty(this.data.another_piece)) {
        wx.showToast({
          title: "请填写续件个数",
          icon: "none"
        });
        return
      }
      if (t._isTextEmpty(this.data.first_piece_price)) {
        wx.showToast({
          title: "请填写首件价格",
          icon: "none"
        });
        return
      }
      if (t._isTextEmpty(this.data.another_piece_price)) {
        wx.showToast({
          title: "请填写续件价格",
          icon: "none"
        });
        return
      }
    }
    if(this.data.template_id){
      this.editTemplate()
    }else {
      this.addTemplate()
    }
    

  }
});